package com.sweet.mealon.face.daily.lock.reentrantlock;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author: crazier.huang
 * @project: faceDaily
 * @date: 2021/4/29 星期四
 * @desc:
 */
public class ReentrantThread implements Runnable {
    private static ReentrantLock lock = new ReentrantLock(false);

    public void run() {
        while (true){
            lock.lock();
            try {
                TimeUnit.SECONDS.sleep(1);
                System.out.println(String.format("%s get lock",Thread.currentThread().getName()));
            } catch (Exception e) {
                e.printStackTrace();
            }finally {
                lock.unlock();
            }
        }
    }

    public static void main(String[] args) {

        for (int i = 0; i < 2; i++) {
            Thread t = new Thread(new ReentrantThread(), "thread-" + i);
            t.start();
        }
    }
}
